package com.haoyun.mirage.user.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.haoyun.common.constant.CommonConstant;
import com.haoyun.common.controller.BaseController;
import com.haoyun.common.core.Result;
import com.haoyun.common.utils.Query;
import com.haoyun.mirage.user.model.dto.MenuDTO;
import com.haoyun.mirage.user.model.dto.RoleDTO;
import com.haoyun.mirage.user.model.entity.SysRole;
import com.haoyun.mirage.user.model.vo.RoleVO;
import com.haoyun.mirage.user.service.ISysMenuService;
import com.haoyun.mirage.user.service.ISysRoleMenuService;
import com.haoyun.mirage.user.service.ISysRoleService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author twg
 * @since 2018-05-30
 */
@RestController
@RequestMapping("/role")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private ISysRoleMenuService roleMenuService;


    /**
     * 通过ID查询角色信息
     *
     * @param id ID
     * @return 角色信息
     */
    @GetMapping("/{id}")
    public RoleVO role(@PathVariable Long id) {
        RoleDTO role = roleService.getRoleWithDeptByRoleId(id);
        RoleVO roleVO = new RoleVO();
        BeanUtil.copyProperties(role, roleVO);
        return roleVO;
    }

    /**
     * 添加角色
     *
     * @param roleVO 角色信息
     * @return success、false
     */
    @PostMapping
    public Result<String> addRole(@RequestBody @Valid RoleVO roleVO, BindingResult bindingResult) {
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            return Result.buildErrorResult(fieldError.getDefaultMessage());
        }
        RoleDTO roleDTO = new RoleDTO();
        BeanUtil.copyProperties(roleVO,roleDTO);
        roleDTO.setCompId(currentUser().getCompId());
        roleService.insertRole(roleDTO);
        return Result.buildSuccessResult("添加成功");
    }

    /**
     * 修改角色
     *
     * @param roleVO 角色信息
     */
    @PutMapping
    public Result<String> roleUpdate(@RequestBody @Valid RoleVO roleVO, BindingResult bindingResult) {
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            return Result.buildErrorResult(fieldError.getDefaultMessage());
        }
        RoleDTO role = new RoleDTO();
        BeanUtil.copyProperties(roleVO, role);
        roleService.updateRole(role);
        return Result.buildSuccessResult("修改成功");
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> roleDel(@PathVariable Long id) {
        return Result.buildSuccessResult(roleService.deleteRoleById(id));
    }

    /**
     * 通过部门ID，获取角色列表
     *
     * @param deptId 部门ID
     * @return 角色列表
     */
    @GetMapping("/roleList/{deptId}")
    public List<RoleDTO> roleList(@PathVariable Long deptId) {
        return roleService.findByDeptId(deptId);

    }

    /**
     * 分页查询角色信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/rolePage")
    public Page rolePage(@RequestParam Map<String, Object> params) {
        params.put("compId", currentUser().getCompId());
        return roleService.findRoleWithDeptByPage(new Query<>(params));
    }

    /**
     * 更新角色菜单
     *
     * @param roleId  角色ID
     * @param menuIds 菜单结合
     * @return success、false
     */
    @PutMapping("/roleMenuUpd")
    public Result<Boolean> roleMenuUpd(@RequestParam("roleId") Long roleId, @RequestParam("menuIds[]") Long[] menuIds) {
        return Result.buildSuccessResult(roleMenuService.insertRoleMenus(roleId, menuIds));
    }

    /**
     * 返回角色的菜单集合
     *
     * @param roleId 角色id
     * @return 属性集合
     */
    @GetMapping("/roleTree/{roleId}")
    public List<String> roleTree(@PathVariable Long roleId) {
        List<MenuDTO> menus = menuService.findMenuByRoleId(roleId);
        List<String> menuList = new ArrayList<>();
        for (MenuDTO menu : menus) {
            menuList.add(menu.getMenuId().toString());
        }
        return menuList;
    }

}

